package SyntaxTree;
import java.util.ArrayList;
import java.util.List;

/*
 * this class implements the count operation
 * 
 * count the simple statement number
 * count the whole parse tree number
 */
public class CounterManager {
	
	private List<String> StatementVector; // store the statement name
	private List<Integer> StatementCounterVector; //store the counter
	private int whole_tree_weight;
	private List<String> TreeVector;
	private List<Integer> TreeWeightVector;
	
	
	public List<Integer> getTreeWeightVector() {
		return TreeWeightVector;
	}
	public void setTreeWeightVector(List<Integer> treeWeightVector) {
		TreeWeightVector = treeWeightVector;
	}
	public List<String> getTreeVector() {
		return TreeVector;
	}
	public void setTreeVector(List<String> treeVector) {
		TreeVector = treeVector;
	}

	public List<String> getStatementVector() {
		return StatementVector;
	}
	public void setStatementVector(List<String> statementVector) {
		StatementVector = statementVector;
	}
	public List<Integer> getStatementCounterVector() {
		return StatementCounterVector;
	}
	public void setStatementCounterVector(List<Integer> statementCounterVector) {
		StatementCounterVector = statementCounterVector;
	}
	public int getWhole_tree_weight() {
		return whole_tree_weight;
	}
	public void setWhole_tree_weight(int whole_tree_weight) {
		this.whole_tree_weight = whole_tree_weight;
	}

	public CounterManager()
	{
		this.StatementCounterVector = new ArrayList<Integer>();
		this.StatementVector = new ArrayList<String>();
		this.TreeVector = new ArrayList<String>();
		this.TreeWeightVector = new ArrayList<Integer>();
		this.whole_tree_weight = 0;
	}

	public void CountWeightOfTree(SimpleNode root){
		whole_tree_weight = dump(root);
	}
	
	private int dump(SimpleNode theNode)
	{
		Node[] children;
		children = theNode.getChildren();
		int weight = 0;
		if (CheckStatement(theNode.getId()))
		{
			weight = 1;
			TreeVector.add(theNode.toString());
			TreeWeightVector.add(weight);
			int pos = FindStatement(theNode.toString());
			if(pos==StatementVector.size())
			{
				InsertStatement(theNode.toString());
			}
			else
			{
				int old = StatementCounterVector.get(pos);
				old = old +1;
				StatementCounterVector.set(pos, old);
			}
		}
		if (children != null && weight == 0)
		{
			for (int i = 0; i < children.length; ++i)
			{
				SimpleNode n = (SimpleNode)children[i];
				if (n != null)
				{
		        	weight += dump(n);
		        }
		     }
			if(theNode.getId() == 15){
				weight *= 2;
				TreeVector.add(theNode.toString());
				TreeWeightVector.add(weight);
			}
			if(theNode.getId() == 16){
				weight *= 4;
				TreeVector.add(theNode.toString());
				TreeWeightVector.add(weight);
			}
		}
		return weight;
	}
	/*
	 * search the tree and give the number of statement and the whole weight of
	 * this parse tree
	 */
	/*
	public void CountSimpleStatement(SimpleNode root){
		Node[] children;
		children = root.getChildren();
		for(int i = 0;i<children.length;i++){
			SimpleNode n = (SimpleNode)children[i];
			if(CheckStatement(n.id))// check if it's a simple statement 
			{
				int pos = FindStatement(n.toString());
				if(pos!=StatementVector.size()){
					InsertStatement(n.toString());
				}
				else
					{int old = StatementCounterVector.get(pos);
					old = old +1;
					StatementCounterVector.set(pos, old);
					}
			}
			CountSimpleStatement(n);
		}
	}
	*/
	public void InsertStatement(String statement){
		this.StatementVector.add(statement);
		this.StatementCounterVector.add(1);
	}
	public int FindStatement(String statement){
		int pos=StatementVector.size();
		for(int i = 0 ; i < StatementVector.size();i++)
		{
			if(StatementVector.get(i).equals(statement))
			{
				pos = i;
				break;
			}
		}
		return pos;
	}
	/*
	 * check the statement if it's simple statement
	 * returnstatement printstatement assignment arrayassignment
	 */
	public boolean CheckStatement(int id){ 
		boolean flag = false;
		switch(id){
		case 5:flag = true; break;
		case 13:flag = true; break;
		case 14:flag = true; break;
		case 17:flag = true; break;
		default: flag = false; break;
		}
		return flag;
	}
	public void printweight(){
		for(int i =0;i<StatementVector.size();i++){
			System.out.println(StatementVector.get(i));
			System.out.println(StatementCounterVector.get(i));
		}
		for(int i=0;i<TreeVector.size();i++){
			System.out.println(TreeVector.get(i));
			System.out.println(TreeWeightVector.get(i));
		}
		System.out.println(this.whole_tree_weight);
	}
}

